/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-02
 * Time: 13:15
 */
public class test2 {
    /*
    NewCoder BM14 链表的奇偶重排
    给定一个单链表，请设定一个函数，将链表的奇数位节点和偶数位节点分别放在一起，重排后输出。
     */
    public ListNode oddEvenList (ListNode head) {
        // write code here
        if(head==null || head.next==null){
            return head;
        }
        // 利用slow代表奇结点
        // 利用fast代表偶结点
        ListNode fast = head.next;
        ListNode slow = head;
        ListNode fastHead = fast;
        while(fast!=null && fast.next!=null){
            slow.next = fast.next;
            slow = slow.next;
            fast.next = slow.next;
            fast = fast.next;
        }
        slow.next = fastHead;
        return head;
    }

    public class ListNode {
        int val;
        ListNode next = null;
       public ListNode(int val) {
          this.val = val;
       }
  }
}
